<?php
class ItemTypeEnum
{
    const normalItem = 0;
    const weapon = 1;
    const iap = 2;
}
class ItemBuyTypeEnum
{
    const game_money = 1;
    const real_money = 2;
    const cash = 4;
}
class ItemService extends BaseService
{

    private $redisObj;
    private $allItemKey;
    static private $_instance = NULL;

    private function  __construct()
    {
        $this->redisObj = Config::getRedisClient();
        $this->allItemKey = "game:shoot:allItem";
    }

    private function  __clone()
    {
    }

    static public function getInstance()
    {
        if (is_null(self::$_instance) || !isset(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

//    public function genGameItemKey($itemId)
//    {
//        return "game:shoot:item:" . $itemId;
//    }
//
//    public function addGameItemKey($itemId, $item)
//    {
//        $key = $this->genGameItemKey($itemId);
//        $this->redisObj->set($key, $item);
//    }
//
//    public function getGameItem($itemId)
//    {
//        $key = $this->genGameItemKey($itemId);
//        $itemStr = $this->redisObj->get($key);
//        return json_decode($itemStr);
//    }


    public function initItemList()
    {
        $itemFileAry = Config::getItemInitFile();
        $version = Config::$settingVerison;
        $item = Item::model()->find('version=:version', array(':version' => $version));
        if (!isset($item)) {
            $fileLocation = $itemFileAry["PropItem"];
            $file = fopen($fileLocation, "r");
            $inx = 1;
            while (!feof($file)) {
                $item = fgetcsv($file);
                if ($inx > 1) {
                    $itemN = new Item();
                    $itemN->item_id = $item[0];
                    $itemN->name = $item[1];
                    $itemN->game_money = (float)$item[4];
                    $itemN->real_money = (float)$item[5];
                    date_default_timezone_set('UTC');
                    $itemN->create_time = date('Y-m-d H:i:s', time());
                    $itemN->version = $version;
                    $itemN->type = ItemTypeEnum::normalItem;
                    $itemN->buy_type =ItemBuyTypeEnum::game_money;
                    $itemN->save();
                };
                $inx++;
            }
            $fileLocationI = $itemFileAry["PropWeapon"];
            $fileI = fopen($fileLocationI, "r");
            $inxI = 1;
            while (!feof($fileI)) {
                $itemI = fgetcsv($fileI);
                if ($inxI > 1) {
                    $itemN = new Item();
                    $itemN->item_id = $itemI[0];
                    $itemN->name = $itemI[1];
                    $itemN->real_money = (float)$itemI[9];
                    date_default_timezone_set('UTC');
                    $itemN->create_time = date('Y-m-d H:i:s', time());
                    $itemN->version = $version;
                    $itemN->type = ItemTypeEnum::weapon;
                    $itemN->buy_type =ItemBuyTypeEnum::real_money;
                    $itemN->save();
                };
                $inxI++;
            }

            $fileLocationII = $itemFileAry["IAPItem"];
            $fileII = fopen($fileLocationII, "r");
            var_dump($fileLocationII);
            $inxII = 1;
            while (!feof($fileII)) {
                $itemII = fgetcsv($fileII);
                if ($inxII > 1) {
                    $itemN = new Item();
                    $itemN->item_id = (int)$itemII[0];
                    $itemN->name = $itemII[1];
                    $temp = new stdClass();
                    $temp->bundleId = $itemII[2];
                    $itemN->properties = json_encode($temp);
                    date_default_timezone_set('UTC');
                    $itemN->create_time = date('Y-m-d H:i:s', time());
                    $itemN->version = $version;
                    $itemN->type = ItemTypeEnum::iap;
                    $itemN->buy_type =ItemBuyTypeEnum::cash;
                    $itemN->save();
                };
                $inxII++;
            }

        }
        //$this->redisObj->del($this->allItemKey);
    }


//type = 0:forward, type=1:backward
    public function getItemList($offset, $count, $type = 0)
    {
        $result = array();
        $allItemValue = $this->redisObj->get($this->allItemKey);
        if (!empty($allItemValue) && isset($allItemValue) && !ShooterService::getInstance()->isNewVersionAvalible()) {
            parent::llog("from cache", "ItemService.getItemList");
            $result = json_decode($allItemValue);
            //usort($result, 'sortById');
            if ($type == 0 && count($result) > $count) {
                $result = UtilityService::getInstance()->arrayForward($result, $offset, $count);
            }
        } else {
            $this->initItemList();
            $itemList = Item::model()->findAll(array("condition"=>"version=:version","params"=>array(":version"=>Config::$settingVerison),"order"=>"id DESC"));
            foreach ($itemList as $item) {
                //var_dump($model->attributes);
                $itemArr = $item->attributes;
                array_walk_recursive($itemArr, function (&$attribute, $key) {
                    if (is_numeric($attribute) && $attribute!==Config::$settingVerison) {
                        $attribute = (int)$attribute;
                    }
                });
                unset($itemArr["create_time"]);
                $result[] = $itemArr;
            }
            $this->redisObj->set($this->allItemKey,json_encode($result));
            $result = $this->redisObj->get($this->allItemKey);
            $result = json_decode($result);
            if ($type == 0 && count($result) > $count) {
                $result = UtilityService::getInstance()->arrayForward($result, $offset, $count);
            }
        }
        return $result;
    }



    public function findItem($itemId,$type)
    {
        $allItems = $this->getItemList(0,100);
        foreach($allItems as $item)
        {
            if((int)$item->item_id == (int)$itemId && (int)$item->type == (int)$type)
            {
                return $item;
            }
        }
    }
}